package testLayer;

import static org.junit.Assert.*;

import java.sql.SQLException;

import modelLayer.Schedule;
import modelLayer.Teacher;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import dbLayer.DBClass;
import dbLayer.DBSchedule;
import dbLayer.DBTeacher;

public class ScheduleDatabaseTest {
	
	private DBSchedule dbSchedule = new DBSchedule();
	private boolean retrieveAssociation = false;
	
	@BeforeClass
	public static void prepareTest() throws Exception {
		
		Teacher t = new DBTeacher().findById(1); 
		new DBClass().insert("men0813", "1", "2013", t);
		
	}
	
	@AfterClass
	public static void cleanUp() throws Exception {
		
		DBClass dbCl = new DBClass();
		dbCl.remove(dbCl.find("men0813", false).getId());
		
	}
	

	@Before
	public void setUp() throws Exception {
		
		testInsert();
		
	}

	@After
	public void tearDown() throws Exception {
		
		testRemove();
		
	}

	@Test
	public void testDBSchedule() {
		
		assertNotNull(dbSchedule);
		
	}

	@Test
	public void testInsert() throws SQLException {
		
		assertTrue(dbSchedule.insert("men0813", 36) > 0);
		
	}

	@Test
	public void testFind() {
		
		if (retrieveAssociation) {
			assertTrue(this.dbSchedule.find("men0813", true).getCl() != null &&
					   this.dbSchedule.find("men0813", true).getBlocks().size() > 0);
		}
		else {
			assertTrue((this.dbSchedule.find("men0813", false) != null) &&
					   (this.dbSchedule.find("men0813", false) instanceof Schedule));			
		}
		
	}

	@Test
	public void testFindById() {
		
		if (retrieveAssociation) {
			assertTrue(this.dbSchedule.findById(dbSchedule.find("men0813", false).getId(), true).getCl() != null &&
					   this.dbSchedule.findById(dbSchedule.find("men0813", false).getId(), true).getBlocks().size() > 0);
		}
		else {
			assertTrue((this.dbSchedule.findById(dbSchedule.find("men0813", false).getId(), false) != null) &&
					   (this.dbSchedule.findById(dbSchedule.find("men0813", false).getId(), false) instanceof Schedule));			
		}
		
	}

	@Test
	public void testGetAll() {
		
		assertTrue(this.dbSchedule.getAll(retrieveAssociation).size() > 0);
		
	}

//	@Test
//	public void testUpdate() {
//	
//		assertTrue(this.dbSchedule.update(className, tbId, blockNo, scheduleId)) > 0);
//	
//	}

	@Test
	public void testRemove() {
		
		assertTrue(dbSchedule.remove("men0813") > 0);
		
	}

}
